﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;
namespace BusinessLogic
{
    public static class TienTe
    {
        public static Entities.TienTe[] Select(Entities.TienTe nv)
        {
            Entities.TienTe[] list = null;
            try
            {
                string sql = Common.Constants.TienTe.SelectTienTe;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.TienTe.HanhDong, SqlDbType.NVarChar).Value = nv.HanhDong;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.TienTe row = new Entities.TienTe();
                    row.TienteID = int.Parse(dr[Common.Constants.TienTe.TienteID].ToString());
                    row.MaTienTe = dr[Common.Constants.TienTe.MaTienTe].ToString();
                    row.TenTienTe = dr[Common.Constants.TienTe.TenTienTe].ToString();
                    row.BieuTuong = dr[Common.Constants.TienTe.BieuTuong].ToString();
                    row.TyGia = dr[Common.Constants.TienTe.TyGia].ToString();
                    row.GhiChu = dr[Common.Constants.TienTe.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.TienTe.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) { list = null; }
                list = new Entities.TienTe[n];
                for (int i = 0; i < n; i++)
                {
                    list[i] = (Entities.TienTe)arr[i];
                }
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); list = null; }
            return list;
        }
        public static int InsertUpdate(Entities.TienTe nhom)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.TienTe.InsertUpdateTienTe, cn);
                cmd.Parameters.Add(Common.Constants.TienTe.HanhDong, SqlDbType.NVarChar).Value = nhom.HanhDong;
                cmd.Parameters.Add(Common.Constants.TienTe.TienteID, SqlDbType.Int).Value = nhom.TienteID;
                cmd.Parameters.Add(Common.Constants.TienTe.MaTienTe, SqlDbType.VarChar, 50).Value = nhom.MaTienTe;
                cmd.Parameters.Add(Common.Constants.TienTe.TenTienTe, SqlDbType.NVarChar, 200).Value = nhom.TenTienTe;
                cmd.Parameters.Add(Common.Constants.TienTe.BieuTuong, SqlDbType.NVarChar, 10).Value = nhom.BieuTuong;
                cmd.Parameters.Add(Common.Constants.TienTe.TyGia, SqlDbType.Float).Value = nhom.TyGia;
                cmd.Parameters.Add(Common.Constants.TienTe.GhiChu, SqlDbType.NVarChar, 200).Value = nhom.GhiChu;
                cmd.Parameters.Add(Common.Constants.TienTe.Deleted, SqlDbType.Bit).Value = nhom.Deleted;
                rt = cmd.ExecuteNonQuery();
                
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }
            return rt;
        }

        public static int Delete(Entities.TienTe nhom)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.TienTe.DeleteTienTe, cn);
                cmd.Parameters.Add(Common.Constants.TienTe.HanhDong, SqlDbType.VarChar).Value = nhom.HanhDong;
                cmd.Parameters.Add(Common.Constants.TienTe.MaTienTe, SqlDbType.VarChar).Value = nhom.MaTienTe;
                rt = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); rt = 0; }
            return rt;
        }
    }
}
